package com.cnc.fd.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.cnc.bas.db.HiberDao;
import com.cnc.bas.db.JdbcDao;
import com.cnc.bas.query.PageInfo;
import com.cnc.bas.query.QuerySql;
import com.cnc.bas.util.Log;
import com.cnc.fd.hiber.MailConfig;

public class MailConfigService {

    public static final String BEAN_ID = "mailConfigService";
    private Log log = Log.getLogger(this.getClass());
    private JdbcDao jdbcDao;
   
    public void queryList(Map hs, PageInfo page) {

        List<Object> params = new ArrayList<Object>();
        StringBuffer sql = new StringBuffer(
                "SELECT t.customer, t.cust_mail,t.sys_mail,t.cust_mail_interval,t.sys_mail_interval ");
        sql.append(" FROM FD_Cust_Mail_Config t where 1=1 ");

        String customerName = (String)hs.get("customer");
        if (StringUtils.isNotEmpty(customerName)) {
            sql.append(" AND t.customer like ? ");
            params.add("%" + customerName + "%");
        }

        QuerySql qs = new QuerySql(sql);
        qs.setValues(params);
        jdbcDao.queryForPage(page, qs);
    }
    
    /**
     * 删除客户邮件配置
     * @param customer
     */
    public void delete(String customer){
        String sql = "delete from FD_Cust_Mail_Config where customer=?";
        jdbcDao.update(sql, new Object[]{customer});
    }
    
    /**
     * 添加客户邮件配置（系统操作）
     * @param customer
     */
    public void add(MailConfig mailConfig){
        String sql = "insert into FD_Cust_Mail_Config(customer, cust_mail, sys_mail, cust_mail_interval, sys_mail_interval) values(?, ?, ?, ?, ?)";
        Object[] data = new Object[]{mailConfig.getCustomer(), mailConfig.getCustMail(), mailConfig.getSysMail(), mailConfig.getCustMailInterval(), mailConfig.getSysMailInterval()};
        jdbcDao.update(sql, data);
    }
    
    /**
     * 添加客户邮件配置（客户操作）
     * @param customer
     */
    public void custAdd(MailConfig mailConfig){
        String sql = "insert into FD_Cust_Mail_Config(customer, cust_mail, cust_mail_interval) values(?, ?, ?)";
        Object[] data = new Object[]{mailConfig.getCustomer(), mailConfig.getCustMail(), mailConfig.getCustMailInterval()};
        jdbcDao.update(sql, data);
    }
    
    /**
     * 根据客户获取邮件配置信息
     * @param customer
     * @return
     */
    public MailConfig getByCustomer(String customer){
        String sql = "select customer, cust_mail, sys_mail, cust_mail_interval, sys_mail_interval from FD_Cust_Mail_Config t where t.customer=?";
        List list = jdbcDao.queryForList(sql, new Object[]{customer});
        if(list == null || list.isEmpty()){
            return null;
        }
        Map data = (Map)list.get(0);
        MailConfig mailConfig = new MailConfig();
        mailConfig.setCustomer((String)data.get("customer"));
        mailConfig.setCustMail((String)data.get("cust_mail"));
        mailConfig.setSysMail((String)data.get("sys_mail"));
        mailConfig.setCustMailInterval((Integer)data.get("cust_mail_interval"));
        mailConfig.setSysMailInterval((Integer)data.get("sys_mail_interval"));
        return mailConfig;
    }
    
    /**
     * 更新用户邮件配置（系统配置）
     * @param mailConfig
     */
    public void update(MailConfig mailConfig){
        String sql = "update FD_Cust_Mail_Config set cust_mail=?, sys_mail=?, cust_mail_interval=?, sys_mail_interval=? where customer=?";
        Object[] data = new Object[]{mailConfig.getCustMail(), mailConfig.getSysMail(), mailConfig.getCustMailInterval(), mailConfig.getSysMailInterval(), mailConfig.getCustomer()};
        jdbcDao.update(sql, data);
    }
    
    /**
     * 更新用户邮件配置（客户配置）
     * @param mailConfig
     */
    public void custUpdate(MailConfig mailConfig){
        String sql = "update FD_Cust_Mail_Config set cust_mail=?, cust_mail_interval=? where customer=?";
        Object[] data = new Object[]{mailConfig.getCustMail(), mailConfig.getCustMailInterval(),  mailConfig.getCustomer()};
        jdbcDao.update(sql, data);
    }

    public JdbcDao getJdbcDao() {
        return jdbcDao;
    }

    public void setJdbcDao(JdbcDao jdbcDao) {
        this.jdbcDao = jdbcDao;
    }
    
    /**
     * 客户是否存在
     * @param customer
     * @return
     */
    public boolean isCustomerExist(String customer){
        String sql = "select count(*)  as totalCount from PW_OPER where OPER_CODE = ?";
        Object[] values = {customer};
        Map map = jdbcDao.queryForMap(sql, values);
        long count = ((Long)map.get("totalCount")).longValue();
        if(count > 0){
            return true;
        }
        return false;
    }

}
